import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hj_crm/ext/get_extension.dart';
import 'package:hj_crm/login/user_login.dart';
import 'api_code.dart';
import 'http_request.dart';
import 'package:hj_crm/log/logger.dart';
// import 'package:healthyelders/utils/toast_utils.dart';
import 'request_api.dart';
import 'request_result_platform.dart';

class Request {
  /// 发起GET请求 平台菜单
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [isJson] 是否json格式
  /// [dialog] 是否显示loading
  /// [token] 是否校验token
  /// [cancelToken] 取消请求token
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static void appGet<T>(
    String url,
    parameters, {
    bool isJson = false,
    bool dialog = true,
    bool token = true,
    bool isLogin = false,
    CancelToken? cancelToken,
    Success<T>? success,
    Fail? fail,
  }) {
    // 设置 appKey 参数
    _request(
      Method.GET,
      url,
      parameters,
      baseUrl: RequestApi.baseUrl,
      isJson: isJson,
      dialog: dialog,
      token: token,
      isLogin: isLogin,
      cancelToken: cancelToken,
      success: success,
      fail: fail,
    );
  }

  /// 发起GET请求 平台菜单
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static void appPost<T>(
    String url,
    parameters, {
    String? loginType,
    bool isJson = true,
    bool dialog = true,
    bool token = true,
    bool isLogin = false,
    CancelToken? cancelToken,
    Success<T>? success,
    Fail? fail,
  }) {
    // 设置 appKey 参数
    _request(
      Method.POST,
      url,
      parameters,
      loginType: loginType,
      baseUrl: RequestApi.baseUrl,
      isJson: isJson,
      dialog: dialog,
      token: token,
      isLogin: isLogin,
      cancelToken: cancelToken,
      success: success!,
      fail: fail!,
    );
  }

  /// 发起请求
  /// [method] 请求类型
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static void _request<T>(
    Method method,
    String url,
    parameters, {
    String? loginType,
    CancelToken? cancelToken,
    String? baseUrl,
    bool isJson = false,
    bool dialog = true,
    bool token = true,
    bool isLogin = false,
    Success<T>? success,
    Fail? fail,
  }) {
    if (dialog) {
      Get.showLoading();
    }
    debugPrint("request url ==============> ${RequestApi.baseUrl}$url");
    if (null != parameters && parameters is Map) {
      debugPrint("请求参数 ==============> ${parameters.toString()}");

      ///校验参数中是否携带URL
      parameters.forEach((key, value) {
        if (url.contains(key)) {
          url = url.replaceAll(':$key', value.toString());
        }
      });
    }

    ///开启请求
    HttpRequest.request(method, url, parameters,
        cancelToken: cancelToken,
        loginType: loginType,
        baseUrl: baseUrl,
        token: token,
        isLogin: isLogin,
        isJson: isJson, success: (result) {
      logger.d(result);
      if (dialog) {
        Get.dismiss();
      }

      var resultModel = RequestResultPlatform.fromJson(result as Map<String, dynamic>);
      debugPrint(
          '<=================请求接口返回错误：错误码：${resultModel.code}, 错误信息：${resultModel.msg} =================>');
      switch (resultModel.code) {
        case ApiCode.API_SUCCESS:
          logger.d(resultModel);
          success?.call(resultModel.data);
          break;

        /// token 失败
        case ApiCode.ERROR_CODE_NOT_LOGIN:
          Get.to(const LoginPageWidget());
          break;
        case ApiCode.ERROR_CODE_NOT_REGISTER:
          Get.to(const LoginPageWidget());
          break;
        default:
          fail!(resultModel.code, resultModel.msg!);
          break;
      }
      //     break;
      // }
    }, fail: (code, msg) {
      debugPrint("request error =>$msg");
      if (dialog) {
        Get.dismiss();
      }
      // ToastUtils.show(msg);
      fail?.call(code, msg);
    });
  }
}
